package com.example.administrator.szb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.adapter.JGDetailAdapter;
import com.example.administrator.szb.bean.GWXQBean;
import com.example.administrator.szb.bean.IndexBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.CheckInfoUtils;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.util.SharedPreferencesUtil;
import com.example.administrator.szb.util.SpannableStringUtil;
import com.example.administrator.szb.util.Toasts;
import com.example.administrator.szb.view.MyListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by D N on 2017/10/17.
 */

public class JGXQActivity extends MVPBaseActivity {
    private LinearLayout activity_jgdetail;
    private com.makeramen.roundedimageview.RoundedImageView img1;
    private TextView name1;
    private TextView address;
    private TextView jgxq_jds;
    private TextView jgxq_scs;
    private TextView jgxq_jyxm;
    private TabLayout tabLayout;
    private MyListView listView;
    private TextView ll_go_ucenter;
    private ImageView iv_1;
    private ImageView iv_2;
    private ImageView iv_3;
    private ImageView iv_4;
    private ImageView iv_5,jgexq_check_img;
    private TextView gwgy,bar_title,company;
    private TextView companyinfo;
    private TextView yewuinfo;
    private CheckBox jgxq_check_sc;
    private TextView jgexq_check_djzk;
    private Button jgxq_button_sqyw;
    private Button jgxq_button_wyzx;
    public static GWXQBean gwjgBean;
    private List<GWXQBean.DataBean.BusinessBean> list = new ArrayList<>();
    private List<GWXQBean.DataBean.BusinessBean.NextBeanX> listTmp = new ArrayList<>();
    private List<GWXQBean.DataBean.BusinessBean.NextBeanX> list2 = new ArrayList<>();
    private int gw_id;
    private JGDetailAdapter adapter;
    private LinearLayout ll_checks;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jgdetail);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        isOpen = false;
        company = findViewById(R.id.company);
        ll_checks = findViewById(R.id.ll_checks);
        bar_title = findViewById(R.id.bar_title);
        bar_title.setText("顾问详情");
        jgexq_check_img = findViewById(R.id.jgexq_check_img);
        activity_jgdetail = (LinearLayout) findViewById(R.id.activity_jgdetail);
        img1 = (com.makeramen.roundedimageview.RoundedImageView) findViewById(R.id.img1);
        name1 = (TextView) findViewById(R.id.name1);
        address = (TextView) findViewById(R.id.address);
        jgxq_jds = (TextView) findViewById(R.id.jgxq_jds);
        jgxq_scs = (TextView) findViewById(R.id.jgxq_scs);
        jgxq_jyxm = (TextView) findViewById(R.id.jgxq_jyxm);
        tabLayout =  findViewById(R.id.tabLayout);
        listView = findViewById(R.id.listView);
        ll_go_ucenter = findViewById(R.id.ll_go_ucenter);
        iv_1 = (ImageView) findViewById(R.id.iv_1);
        iv_2 = (ImageView) findViewById(R.id.iv_2);
        iv_3 = (ImageView) findViewById(R.id.iv_3);
        iv_4 = (ImageView) findViewById(R.id.iv_4);
        iv_5 = (ImageView) findViewById(R.id.iv_5);
        gwgy = (TextView) findViewById(R.id.gwgy);
        jgexq_check_djzk = findViewById(R.id.jgexq_check_djzk);
        companyinfo = (TextView) findViewById(R.id.companyinfo);
        yewuinfo = (TextView) findViewById(R.id.yewuinfo);
        jgxq_check_sc = (CheckBox) findViewById(R.id.jgxq_check_sc);
        jgxq_button_sqyw = (Button) findViewById(R.id.jgxq_button_sqyw);
        jgxq_button_wyzx = (Button) findViewById(R.id.jgxq_button_wyzx);
        //金融顾问已承诺遵守《金融顾问公约》， 如有问题请及时联系客服
        SpannableStringUtil stringUtil = new SpannableStringUtil(gwgy);
        stringUtil.addText("金融顾问已承诺遵守");
        stringUtil.addTextAll("《金融顾问公约》", getResources().getColor(R.color.basecolor), new SpannableStringUtil.ClickListener() {
            @Override
            public void onClick() {
                Intent intent_web = new Intent(context, GZXYActivity.class);//额，
                intent_web.putExtra("title", "金融顾问公约");
                intent_web.putExtra("id", 5);
                startActivity(intent_web);
            }
        });
        stringUtil.addText("， 如有问题请及时联系客服");
        gwgy.setText(stringUtil.getText());
    }

    @Override
    protected void initEvent() {
        gw_id = getIntent().getIntExtra("id", -1);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                initTab(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        jgxq_check_sc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dosc();
            }
        });

        jgxq_button_wyzx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SampleApplicationLike.getUserloginInstance().getId() == gw_id) {
                    Toasts.show(context, "不可以向自己咨询", Toast.LENGTH_SHORT);
                } else {
                    if (CheckInfoUtils.isLogin(context)){
                        if (gwjgBean==null||gwjgBean.getData()==null){
                            return;
                        }
                        Intent intent = new Intent(context, AskNewActivity.class);
                        //intent.putExtra("who", FragmentsFlag.Activity_JGXQ.ordinal());
                        intent.putExtra("gw_id", gwjgBean.getData().getUsers_id() + "");
                        startActivity(intent);
                    }

                }
            }
        });
        ll_go_ucenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (gwjgBean!=null&&gwjgBean.getData()!=null){
                    Intent intent_go = new Intent(context, GWCententActivity.class);
                    intent_go.putExtra("refer_id", gwjgBean.getData().getUsers_id());
                    startActivity(intent_go);
                }

            }
        });
        jgxq_button_sqyw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SampleApplicationLike.getUserloginInstance().getId() == gw_id) {
                    Toasts.show(context, "不可以向自己发起项目", Toast.LENGTH_SHORT);
                }
                else {
                    if (!CheckInfoUtils.isLogin(context)){
                        return;
                    }
                    int id_type = getIntent().getIntExtra("id_type", -1);
                    if (id_type == -1) {
                        Intent intent = new Intent(context, XZGWYWActivity.class);
                        intent.putExtra("counselor_id", gw_id);
                        intent.putExtra("title", "提交资料");
                        intent.putExtra("id", id_type);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(context, SQXMActivity.class);
                        //intent.putExtra("title",title);
                        intent.putExtra("id", id_type);
                        int type = 0;
                        int genre = 2;
                        IndexBean indexBean=(IndexBean) SharedPreferencesUtil.getData(context,"index");
                        for (int i = 0; i < indexBean.getData().getBusiness().size(); i++) {
                            if (indexBean.getData().getBusiness().get(i).getId() == id_type) {
                                genre = indexBean.getData().getBusiness().get(i).getGenre();
                                type = indexBean.getData().getBusiness().get(i).getType();
                            }
                        }
                        intent.putExtra("type", type);
                        intent.putExtra("genre", genre);
                        intent.putExtra("counselor_id", gw_id);
                        startActivity(intent);

                    }
                }}});


        ll_checks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOpen){
                    isOpen = false;
                }else {
                    isOpen = true;
                }
                initCheckView(isOpen,false);
            }
        });
    }
    private boolean isOpen = false;
private void initTab(int pos){
    String str = gwjgBean.getData().getBusiness().get(pos).getDescribe();
    GWXQBean.DataBean.BusinessBean.NextBeanX nextBeanX = null;
    if (!TextUtils.isEmpty(str)){
        //如果描述不为空，将其加入List
        nextBeanX = new GWXQBean.DataBean.BusinessBean.NextBeanX();
        List<GWXQBean.DataBean.BusinessBean.NextBeanX.NextBean> nextBeen = new ArrayList<GWXQBean.DataBean.BusinessBean.NextBeanX.NextBean>();
        GWXQBean.DataBean.BusinessBean.NextBeanX.NextBean bean = new GWXQBean.DataBean.BusinessBean.NextBeanX.NextBean();
        bean.setTitle(str);
        nextBeen.add(bean);
        nextBeanX.setTitle("补充描述");
        nextBeanX.setNext(nextBeen);
    }
    listTmp.clear();
    listTmp.addAll(list.get(pos).getNext());
    if (listTmp.size()>3){
        ll_checks.setVisibility(View.VISIBLE);
    }else {
        ll_checks.setVisibility(View.GONE);
    }
    list2.clear();
    list2.addAll(list.get(pos).getNext());
    if (nextBeanX!=null){
        listTmp.add(nextBeanX);
        list2.add(nextBeanX);
    }
    initCheckView(isOpen,true);

}
    private void initCheckView(boolean b,boolean isTabClick){
        list2.clear();
        if (b){
            //Toasts.show(context, "展开", Toast.LENGTH_SHORT);
            if (!isTabClick){

                jgexq_check_djzk.setText("点击收起");
                jgexq_check_img.setImageResource(R.drawable.djsc_more);
            }

            list2.addAll(listTmp);
        }else{
            if (!isTabClick){

                //Toasts.show(context, "收起", Toast.LENGTH_SHORT);
                jgexq_check_djzk.setText("点击展开");
                jgexq_check_img.setImageResource(R.drawable.djzk_zk);

            }
            for (int i=0;i<listTmp.size();i++){
                if (i<3){
                    list2.add(listTmp.get(i));
                }

            }
        }
        adapter.notifyDataSetChanged();
    }
    @Override
    protected void initData() {
        //list2.add(null);
        adapter = new JGDetailAdapter(list2,context);
        listView.setAdapter(adapter);
        requestData();
    }

    private void initViews(){

        tabLayout.removeAllTabs();

        for (int i=0;i<list.size();i++){
            tabLayout.addTab(tabLayout.newTab().setText(list.get(i).getTitle()));
        }

        if (!TextUtils.isEmpty(gwjgBean.getData().getZhenshi_img())) {
            Glide.with(this).load(gwjgBean.getData().getZhenshi_img()).error(R.mipmap.default_header_s).into(img1);
        }
        /*if (!TextUtils.isEmpty(gwjgBean.getData().getZhenshi_img())) {
            Glide.with(this).load(gwjgBean.getData().getZhenshi_img()).error(R.mipmap.default_header_s).into(img2);
        }*/
        name1.setText(gwjgBean.getData().getName().replace("-",""));
        address.setText(gwjgBean.getData().getCity_id());
        if (gwjgBean.getData().getJigou_introduce()!=null)
        companyinfo.setText(gwjgBean.getData().getJigou_introduce());
        company.setText(gwjgBean.getData().getCompany());
        if (gwjgBean.getData().getYewu_introduce()!=null)
        yewuinfo.setText(gwjgBean.getData().getYewu_introduce());
        jgxq_jds.setText(gwjgBean.getData().getOrderlen() + "");
        jgxq_scs.setText(gwjgBean.getData().getLeft_attention_len() + "");
        jgxq_jyxm.setText(gwjgBean.getData().getBuslen() + "");
        if (gwjgBean.getData().getIsCollect()==1){
            jgxq_check_sc.setText("已收藏");

            jgxq_check_sc.setChecked(true);
        }else {
            jgxq_check_sc.setText("收藏");

            jgxq_check_sc.setChecked(false);
        }
        showStar(gwjgBean.getData().getStar());
    }



    private void requestData() {

        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("users_id", gw_id + "");
        map.put("company_id",""+ SPUtils.getUserId());
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, GWXQBean.class, URLAddress.URLBASE + URLAddress.URL_GWXQ, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                gwjgBean = (GWXQBean) response;
                if (gwjgBean.getResult() == 1) {
                    List<GWXQBean.DataBean.BusinessBean> l = gwjgBean.getData().getBusiness();
                    if (l==null) {
                        l = new ArrayList<GWXQBean.DataBean.BusinessBean>();
                    }
                    list.clear();
                    list.addAll(l);
                    initViews();

                }
            }


        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }
    /**
     * 去收藏
     */
    private void dosc(){
        if (!(SPUtils.isLogin())){
            jgxq_check_sc.setText("收藏");
            jgxq_check_sc.setChecked(false);
            startActivity(new Intent(context,LoginNewActivity_two.class));
            return;
        }
        if (gw_id==SPUtils.getUserId()){
            jgxq_check_sc.setText("收藏");
            jgxq_check_sc.setChecked(false);
            DialogUtil.showToast(context,"不能收藏自己");
            return;
        }
        Map<String,Object> params = new HashMap<>();
        params.put("code",HttpUtil.getParam());
        params.put("users_id", SPUtils.getUserId());
        params.put("company_id",gw_id );

        HttpUtils.post(activity,URLAddress.URLBASE + URLAddress.JG_SC, params, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                Toasts.show(context,str,1000);
                if (str.equals("取消收藏")){
                    jgxq_check_sc.setText("收藏");
                    jgxq_check_sc.setChecked(false);
                    jgxq_check_sc.setClickable(true);
                }else {
                    jgxq_check_sc.setText("已收藏");
                    jgxq_check_sc.setChecked(true);
                    jgxq_check_sc.setClickable(true);

                }
               // requestData();
            }

            @Override
            public void onFailure(int requestCode, String str) {
                jgxq_check_sc.setText("收藏");
                jgxq_check_sc.setChecked(false);
                jgxq_check_sc.setClickable(true);
            }
        });
    }
    private void showStar(int star){
        switch (star){
            case 10:
                iv_1.setImageResource(R.drawable.start);
                iv_2.setImageResource(R.drawable.start);
                iv_3.setImageResource(R.drawable.start);
                iv_4.setImageResource(R.drawable.start);
                iv_5.setImageResource(R.drawable.start);
                break;
            case 9:

                iv_1.setImageResource(R.drawable.start);
                iv_2.setImageResource(R.drawable.start);
                iv_3.setImageResource(R.drawable.start);
                iv_4.setImageResource(R.drawable.start);
                iv_5.setImageResource(R.drawable.star_half);
                break;
            case 8:
                iv_1.setImageResource(R.drawable.start);
                iv_2.setImageResource(R.drawable.start);
                iv_3.setImageResource(R.drawable.start);
                iv_4.setImageResource(R.drawable.start);
                iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 7:
                iv_1.setImageResource(R.drawable.start);
                iv_2.setImageResource(R.drawable.start);
                iv_3.setImageResource(R.drawable.start);
                iv_4.setImageResource(R.drawable.star_half);
                iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 6:

                iv_1.setImageResource(R.drawable.start);
                iv_2.setImageResource(R.drawable.start);
                iv_3.setImageResource(R.drawable.start);
                iv_4.setImageResource(R.drawable.star_hui);
                iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 5:
                iv_1.setImageResource(R.drawable.start);
                iv_2.setImageResource(R.drawable.start);
                iv_3.setImageResource(R.drawable.star_half);
                iv_4.setImageResource(R.drawable.star_hui);
                iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 4:
                iv_1.setImageResource(R.drawable.start);
                iv_2.setImageResource(R.drawable.start);
                iv_3.setImageResource(R.drawable.star_hui);
                iv_4.setImageResource(R.drawable.star_hui);
                iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 3:
                iv_1.setImageResource(R.drawable.start);
                iv_2.setImageResource(R.drawable.star_half);
                iv_3.setImageResource(R.drawable.star_hui);
                iv_4.setImageResource(R.drawable.star_hui);
                iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 2:
                iv_1.setImageResource(R.drawable.start);
                iv_2.setImageResource(R.drawable.star_hui);
                iv_3.setImageResource(R.drawable.star_hui);
                iv_4.setImageResource(R.drawable.star_hui);
                iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 1:

                iv_1.setImageResource(R.drawable.star_half);
                iv_2.setImageResource(R.drawable.star_hui);
                iv_3.setImageResource(R.drawable.star_hui);
                iv_4.setImageResource(R.drawable.star_hui);
                iv_5.setImageResource(R.drawable.star_hui);
                break;
            case 0:
                iv_1.setImageResource(R.drawable.star_hui);
                iv_2.setImageResource(R.drawable.star_hui);
                iv_3.setImageResource(R.drawable.star_hui);
                iv_4.setImageResource(R.drawable.star_hui);
                iv_5.setImageResource(R.drawable.star_hui);
                break;
        }

    }


}

